package com.player.thirdGroup.service;

import com.player.thirdGroup.dao.RoleUserDao;
import com.player.thirdGroup.dao.UserDao;
import com.player.thirdGroup.entity.Role;
import com.player.thirdGroup.entity.RoleUser;
import com.player.thirdGroup.entity.User;
import com.player.thirdGroup.util.BootStrapTablePage;
import com.player.thirdGroup.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author VULCAN
 * @create 2019-12-27 21:36
 * @desc 账号
 **/
@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Autowired
    private IdWorker idWorker;

    public User login(User user) {
        //判断密码一不一致
        User user1 = userDao.findByUsername(user.getUsername());
       if(user1!=null){
           if (user1.getUserpwd().equals(user.getUserpwd())){
               System.err.println(user1+"--------------");
               return user1;
           }
       }
        //不一致
        return null;
    }
    //全部信息
    public Page<User> findAll(BootStrapTablePage page){
       //分页
        PageRequest of = PageRequest.of(page.getPage()-1,page.getLimit());
        Page<User> all = userDao.findAll(of);
        return  all;
    }
    //编辑
    public void update(User user){
        userDao.save(user);
    }
    //修改状态
    public void updateStatus(String userid,String userstatus){
        User user = userDao.findById(userid).get();
        if (userstatus.equals("0")){
            user.setUserstatus("1");
        }
        if(userstatus.equals("1")){
            user.setUserstatus("0");
        }
        userDao.save(user);
    }

    public void zhuce(User user) {
        user.setUserid(idWorker.nextId()+"");
        userDao.save(user);
        //为用户设置角色
//        RoleUser roleUser = new RoleUser();
//        roleUser.setUserid(user.getUserid());
//        roleUser.setRoleid("2");
        roleUserDao.InsertRoleUser(user.getUserid(),"2");
    }

    public User getUserByPhone(String userphone) {
        return userDao.findByUserPhone(userphone);
    }
//    public Page<User> findAll(Specification<User> query, Pageable pageable) {
//        PageRequest of = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize());
//        userDao.findAll(new Specification<User>() {
//            @Override
//            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
//                return null;
//            }
//        },of);
//        return null;
//    }

//    public Page<User> findAll(Specification<User> query, Pageable pageable) {
//
//        return null;
//    }
    /**
     * 根据id查询用户
     * @param userid
     * @return
     */
    public User findByuser(String userid) {
        User user = userDao.findById(userid).get();
        return user;
    }

    public void AddUser(User user) {
        user.setUserid(idWorker.nextId()+"");
        userDao.save(user);
        RoleUser roleUser = new RoleUser();
        roleUser.setUserid(user.getUserid());
        roleUser.setRoleid("2");
        //为用户设置角色
       roleUserDao.save(roleUser);

    }
}
